package com.linchong.concurrency.basic;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author linchong
 * @version 1.0
 * @Date: 2020-12-08 11:16
 * @Description: CountExample$
 */
@Slf4j
public class CountExample {

	/**
	 * 请求处理次数，每次允许200次请求同时执行
	 */
	private static final int THREAD_TOTAL = 200;


	/**
	 * 短时间内接收的总的请求数
	 */
	private static final int CLIENT_TOTAL = 5000;

	private static long count = 0;

	public static void main(String[] args) {
		ExecutorService exec = Executors.newCachedThreadPool();
		final Semaphore semaphore = new Semaphore(THREAD_TOTAL);
		for (int i = 0; i < CLIENT_TOTAL; i++) {
			exec.execute(()->{
				try {
					semaphore.acquire();
					add();
					semaphore.release();
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		}
		exec.shutdown();
		log.info("count:{}",count);
		Object object = new Object();
	}

	private static  void add(){
		count++;
	}
}
